﻿/*******************************************************
 *  帮扶就业 相关操作js
 * <p>Title: helpJobManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2017年8月22日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//帮扶就业
var $table, addOrEditUrl, options,
    user_id, user_name;
var isFrish = false;//默认关闭页面时不刷新
var helpJobManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        if (!helpJobManager.initData()) {
            layer.msg("参数异常！");
            return false;
        }
        helpJobManager.initTable();
        helpJobManager.bindEvent();
        parent.delLoadBoxs();
    },
    //初始化参数
    initData:function(){
        user_id = $.getUrls("user_id");
        if (!user_id)
            return false;
        $('#user_id').val(user_id);
        user_name = decodeURI($.getUrls("user_name"));
        $('#pg_user').val(user_name);
        return true;
    },
    //初始化Table
    initTable: function () {
        options = {
            url: ctx + "/BaneJobIssue/GetSingleBaneJobData",
            cutHeight: 5,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            toolbar: "#exampleToolbar",
            queryParams: function (params) {
                params = {
                    rows: params.limit,
                    //页面大小  
                    page: params.offset / params.limit + 1,
                    user_id: user_id
                }
                return params;
            },
            columns: [
            {
                radio: true
            },
	        {
	            field: '',//第一列序号
	            title: '序号',
	            align: 'center',
	            width: 50,
	            formatter: function (value, row, index) {
	                return index + 1;
	            }
	        },
            {
                field: 'user_name',
                title: '用户名',
                align: 'center'
            },
	        {
	            field: 'user_identify',
	            title: '身份证',
	            align: 'center'
	        },
	        {
	            field: 'help_content',
	            title: '记录概况',
	            align: 'center',
	            formatter: function (value, row, index) {
	                if (value && value.length > 30)
	                    return "<span title='" + value + "'>" + value.substring(0, 30) + "...</span>";
	                return value;
	            }
	        },
            {
                field: 'enter_date',
                title: '登记时间',
                align: 'center'
            }]
        }
        $table = tableHelper.initTable("exampleTableToolbar", options);
    },
    //绑定默认事件
    bindEvent: function () {
        //查询
        $('#btnSearch').click(function () {
            $table.bootstrapTable('refresh', options);
        });
        //查看
        $('#jobDetail').click(function () {
            var row = helpJobManager.getSelect();
            if (!row) {
                layer.msg("未选中行~", { icon: 5 });
                return false;
            }
            var index = layer.open({
                id: "ifreamprintObj",
                title: ['打印就业情况反馈', 'font-size:18px;'],
                type: 2,
                content: ctx + '/BaneJobIssue/SingleHelpJobReport?help_id=' + row.help_id,
                scroll: true,//是否显示滚动条、默认不显示
                btn: ['打印就业情况反馈'],
                area: ['800px', '98%'],
                yes: function (li, o) {
                    var ifameId = $('#ifreamprintObj iframe');
                    window.frames[ifameId.attr('id')].focus();
                    window.frames[ifameId.attr('id')].print();
                }
            });
        });
        //添加
        $('#jobAdd').click(function () {

            helpJobManager.editForm();
        });
        //编辑
        $('#jobEdit').click(function () {
            var row = helpJobManager.getSelect();
            if (!row) {
                layer.msg("未选中行~", { icon: 5 });
                return false;
            }
            helpJobManager.editForm(row);
        });
        //删除
        $('#jobDel').click(function () {
            var row = helpJobManager.getSelect();
            if (!row) {
                layer.msg("未选中记录行~", { icon: 5 });
                return false;
            }
            layer.confirm('您确定要删除当前选中帮扶就业信息吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                $.ajax({
                    type: 'post',
                    url: ctx + '/BaneJobIssue/delHelpJobData/' + row.help_id,
                    dataType: 'json',
                    async: false,
                    success: function (data) {
                        if (index)
                            layer.close(index);
                        if (data.Statu === 0) {
                            isFrish = true;
                            layer.msg("删除成功...");
                            $table.bootstrapTable('refresh', options);
                        }
                        else
                            layer.msg(data.Msg, { icon: 5 });
                    }
                });
            }, function () { });
        });
    },
    //获取选中行
    getSelect: function () {
        var row = $table.bootstrapTable('getSelections');
        if (row != null && row.length == 0) {
            layer.msg("未选中行~", { icon: 5 });
            return false;
        }
        return row[0];
    },
    //编辑、添加
    editForm: function (row) {
        var title = "添加帮扶就业信息";
        if (!row)
            addOrEditUrl = ctx + "/BaneJobIssue/AddHelpJobData";//新增
        else {
            title = "编辑帮扶就业信息";
            addOrEditUrl = ctx + "/BaneJobIssue/EditHelpJobData";//编辑
        }
        //清空表单
        $('#orgFormTable').resetHideValidForm();
        //打开编辑
        layer.open({
            title: [title, 'font-size:18px;'],
            type: 1,
            content: $('#org_form'),
            scroll: true,//是否显示滚动条、默认不显示
            btn: ['确定', '取消'],
            area: ['1050px', '90%'],
            success: function (li, o) {
                if (row != null) {
                    $('#orgFormTable')[0].reset();//重置表单
                    $('#orgFormTable').LoadForm(row);//表单填充数据
                    $('#enter_date').val(row.enter_date_value);
                }
            },
            yes: function (li, o) {
                if ($('#orgFormTable').valid()) {
                    //验证通过
                    $('#orgFormTable').ajaxSubmit({
                        url: addOrEditUrl,
                        type: "post",
                        dataType: "json",
                        beforeSubmit: function (arr, $form, options) {
                            layer.msg("提交数据~", { icon: 1, time: 6000 });
                        },
                        success: function (result, status, xhr, $form) {
                            isFrish = true;
                            if (result.Statu === 0) {
                                layer.closeAll();
                                $table.bootstrapTable('refresh', options);
                                layer.msg('保存成功', { icon: 1 });
                            }
                            else
                                layer.alert(result.Msg, { icon: 5 });
                        },
                        error: function (xhr, status, error, $form) {
                            layer.msg("保存失败~", { icon: 5 });
                        }
                    });
                }
            },
            cancel: function (li, o) {

            }
        });
    }
}

